/**
 * 基于jQuery的通用js方法扩展
 */
(function ($) {
    $.extend({
      // 弹出层封装处理
    	modal: {
        /** 弹窗状态码 */
        STATUS: {
            SUCCESS: "success",
            FAIL: "error",
            WARNING: "warning"
        },
    		// 显示图标
    		icon: function(type) {
          var icon = "";
          if (type == $.modal.STATUS.WARNING) {
              icon = 0;
          } else if (type == $.modal.STATUS.SUCCESS) {
              icon = 1;
          } else if (type == $.modal.STATUS.FAIL) {
              icon = 2;
          } else {
              icon = 3;
          }
          return icon;
        },
    		// 消息提示
        msg: function(content, type, callback) {
          if (type != undefined) {
                layer.msg(content, { icon: $.modal.icon(type), time: 1000, shift: 5 }, function (index) {
                    (typeof callback === "function") && callback(true);
                });
            } else {
                layer.msg(content, function (index) {
                    (typeof callback === "function") && callback(true);
                });
            }
        },
        // 错误消息
        msgError: function(content, callback) {
          $.modal.msg(content, $.modal.STATUS.FAIL, callback);
        },
        // 成功消息
        msgSuccess: function(content, callback) {
          $.modal.msg(content, $.modal.STATUS.SUCCESS, callback);
        },
        // 警告消息
        msgWarning: function(content, callback) {
          $.modal.msg(content, $.modal.STATUS.WARNING, callback);
        },
        // 弹出提示
        alert: function(content, type, callback) {
          layer.alert(content, {
              icon: $.modal.icon(type),
              title: "系统提示",
              btn: ['确认'],
              btnclass: ['btn btn-primary']
          }, function (index) {
                layer.close(index);
                (typeof callback === "function") && callback(true);
            });
        },
        // 消息提示并刷新父窗体
        msgReload: function(msg, type) {
          layer.msg(msg, {
              icon: $.modal.icon(type),
              time: 500,
              shade: [0.1, '#8F8F8F']
          },
          function() {
              $.modal.reload();
          });
        },
        // 错误提示
        alertError: function(content, callback) {
          $.modal.alert(content, $.modal.STATUS.FAIL, callback);
        },
        // 成功提示
        alertSuccess: function(content, callback) {
          $.modal.alert(content, $.modal.STATUS.SUCCESS, callback);
        },
        // 警告提示
        alertWarning: function(content, callback) {
          $.modal.alert(content, $.modal.STATUS.WARNING, callback);
        },
        // 关闭窗体
        close: function () {
          var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },
        // 关闭全部窗体
        closeAll: function () {
            layer.closeAll();
        },
        // 确认窗体
        confirm: function (content, callBack) {
          layer.confirm(content, {
              icon: 3,
              title: "系统提示",
              btn: ['确认', '取消']
          }, function (index) {
            layer.close(index);
              callBack(true);
          });
        },
        // 弹出层指定宽度
        open: function (title, url, width, height, callback) {
          //如果是移动端，就使用自适应大小弹窗
          if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
              width = 'auto';
              height = 'auto';
          }
          if ($.common.isEmpty(title)) {
                title = false;
            };
            if ($.common.isEmpty(url)) {
                url = "/404.html";
            };
            if ($.common.isEmpty(width)) {
              width = 800;
            };
            if ($.common.isEmpty(height)) {
              height = ($(window).height() - 50);
            };
            if ($.common.isEmpty(callback)) {
                callback = function(index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler();
                }
            }
          layer.open({
            type: 2,
            area: [width + 'px', height + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: title,
            content: url,
            btn: ['确定', '关闭'],
              // 弹层外区域关闭
            shadeClose: true,
            yes: callback,
              cancel: function(index) {
                  return true;
              }
          });
        },
        // 弹出层指定参数选项
        openOptions: function (options) {
          var _url = $.common.isEmpty(options.url) ? "/404.html" : options.url;
          var _title = $.common.isEmpty(options.title) ? "系统窗口" : options.title;
            var _width = $.common.isEmpty(options.width) ? "800" : options.width;
            var _height = $.common.isEmpty(options.height) ? ($(window).height() - 50) : options.height;
            var _btn = ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'];
            layer.open({
                type: 2,
            maxmin: true,
                shade: 0.3,
                title: _title,
                fix: false,
                area: [_width + 'px', _height + 'px'],
                content: _url,
                shadeClose: true,
                btn: $.common.isEmpty(options.btn) ? _btn : options.btn,
                yes: function (index, layero) {
                    options.callBack(index, layero)
                }, cancel: function () {
                    return true;
                }
            });
        },
        // 弹出层全屏
        openFull: function (title, url, width, height) {
          //如果是移动端，就使用自适应大小弹窗
          if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
              width = 'auto';
              height = 'auto';
          }
          if ($.common.isEmpty(title)) {
                title = false;
            };
            if ($.common.isEmpty(url)) {
                url = "/404.html";
            };
            if ($.common.isEmpty(width)) {
              width = 800;
            };
            if ($.common.isEmpty(height)) {
              height = ($(window).height() - 50);
            };
            var index = layer.open({
            type: 2,
            area: [width + 'px', height + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: title,
            content: url,
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: function(index, layero) {
                  var iframeWin = layero.find('iframe')[0];
                  iframeWin.contentWindow.submitHandler();
              },
              cancel: function(index) {
                  return true;
              }
          });
            layer.full(index);
        },
        // 禁用按钮
        disable: function() {
          var doc = window.top == window.parent ? window.document : window.parent.document;
        $("a[class*=layui-layer-btn]", doc).addClass("layer-disabled");
        },
        // 启用按钮
        enable: function() {
          var doc = window.top == window.parent ? window.document : window.parent.document;
          $("a[class*=layui-layer-btn]", doc).removeClass("layer-disabled");
        },
        // 打开遮罩层
        loading: function (message) {
          $.blockUI({ message: '<div class="loaderbox"><div class="loading-activity"></div> ' + message + '</div>' });
        },
        // 关闭遮罩层
        closeLoading: function () {
          setTimeout(function(){
            $.unblockUI();
          }, 50);
        },
        // 重新加载
        reload: function () {
          parent.location.reload();
        }
    },
      // 操作封装处理
      operate: {
        // 提交数据
        submit: function(url, type, dataType, data, callback) {
            var config = {
                url: url,
                type: type,
                dataType: dataType,
                data: data,
                beforeSend: function () {
                  $.modal.loading("正在处理中，请稍后...");
                },
                success: function(result) {
                  if (typeof callback == "function") {
                    callback(result);
                  } else {
                    $.operate.ajaxSuccess(result);
                  }
                }
            };
            $.ajax(config)
          },
          // post请求传输
          post: function(url, data, callback) {
            $.operate.submit(url, "post", "json", data, callback);
          },
          // get请求传输
          get: function(url, callback) {
            $.operate.submit(url, "get", "json", "", callback);
          },
          // 保存结果弹出msg刷新table表格
          ajaxSuccess: function (result) {
            if (result.code == 200) {
                $.modal.msgSuccess(result.msg);
            }  else {
              $.modal.alertError(result.msg);
            }
            $.modal.closeLoading();
          },
      },
      // 通用方法封装处理
    	common: {
    		// 判断字符串是否为空
            isEmpty: function (value) {
                if (value == null || this.trim(value) == "") {
                    return true;
                }
                return false;
            },
            // 判断一个字符串是否为非空串
            isNotEmpty: function (value) {
            	return !$.common.isEmpty(value);
            },
            // 空对象转字符串
            nullToStr: function(value) {
                if ($.common.isEmpty(value)) {
                    return "-";
                }
                return value;
            },
            // 是否显示数据 为空默认为显示
            visible: function (value) {
                if ($.common.isEmpty(value) || value == true) {
                    return true;
                }
                return false;
            },
            // 空格截取
            trim: function (value) {
                if (value == null) {
                    return "";
                }
                return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
            },
            // 比较两个字符串（大小写敏感）
            equals: function (str, that) {
            	return str == that;
            },
            // 比较两个字符串（大小写不敏感）
            equalsIgnoreCase: function (str, that) {
            	return String(str).toUpperCase() === String(that).toUpperCase();
            },
            // 将字符串按指定字符分割
            split: function (str, sep, maxLen) {
            	if ($.common.isEmpty(str)) {
            	    return null;
            	}
            	var value = String(str).split(sep);
            	return maxLen ? value.slice(0, maxLen - 1) : value;
            },
            // 字符串格式化(%s )
            sprintf: function (str) {
                var args = arguments, flag = true, i = 1;
                str = str.replace(/%s/g, function () {
                    var arg = args[i++];
                    if (typeof arg === 'undefined') {
                        flag = false;
                        return '';
                    }
                    return arg;
                });
                return flag ? str : '';
            },
            // 指定随机数返回
            random: function (min, max) {
                return Math.floor((Math.random() * max) + min);
            },
            // 判断字符串是否是以start开头
            startWith: function(value, start) {
                var reg = new RegExp("^" + start);
                return reg.test(value)
            },
            // 判断字符串是否是以end结尾
            endWith: function(value, end) {
                var reg = new RegExp(end + "$");
                return reg.test(value)
            },
            // 数组去重
            uniqueFn: function(array) {
                var result = [];
                var hashObj = {};
                for (var i = 0; i < array.length; i++) {
                    if (!hashObj[array[i]]) {
                        hashObj[array[i]] = true;
                        result.push(array[i]);
                    }
                }
                return result;
            },
            // 数组中的所有元素放入一个字符串
            join: function(array, separator) {
            	if ($.common.isEmpty(array)) {
            	    return null;
            	}
                return array.join(separator);
            }
        }
    });
})(jQuery);
